home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / OOPTUT34.ZIP / TVISION.TXT < prev    next >
Text File  |  1993-06-12  |  22KB  |  444 lines

  1.                         TURBO VISION.
  2.                         ------------- 
  3.       
  4. Introduction.
  5. -------------
  6.  
  7.  Since Turbo Vision makes use of object-oriented techniques, including
  8.  inheritance and polymorphism, it is necessary to be completely familiar
  9.  with object-oriented programming.  Furthermore the object instances are
  10.  dynamically allocated on the heap, so that familiarity with pointers and
  11.  dynamic variables is also essential.  The extended syntax of the 'New'
  12.  procedure, which allows the allocation of space on the heap for an object
  13.  and its initialization within the one procedure must also be appreciated.
  14.  This new procedure is now invoked with two parameters, the pointer name as
  15.  the first parameter and the constructor invocation as the second parameter.
  16.  All these topics are covered in the notes on Advanced Data Structures and
  17.  Object-Oriented Programming.
  18.  
  19.  In OOP parlance, an 'object type' is an abstraction that provides a
  20.  template for specific objects, which are themselves called 'instances' of
  21.  that object type.  In Turbo Vision, the templates or object types all start
  22.  with the letter T, whilst all pointer types start with the letter P. Both
  23.  the object instances and the actual pointer variables have names
  24.  appropriate to the circumstances of use.  Thus typical variable
  25.  declarations are:
  26.   VAR
  27.    Window: PDemoWindow;         {see page 32 of the Turbo Vision Guide}
  28.    R: TRect;
  29.  The extended syntax of 'New' also allows it to be used as a function,
  30.  returning a pointer value.  An example taken from page 32 of the Guide is:
  31.  
  32.       Window := New(PDemoWindow,    Init( R,   'Demo Window',WinCount));
  33.         |            |             /       \         |           |
  34.      Pointer      Pointer      Constructor  Object  Title      Number
  35.      variable     type         invocation   of type
  36.                                                TRect
  37.  
  38.  Thus Window is a pointer of type PDemoWindow, which itself has been
  39.  previously declared as:
  40.     TYPE
  41.       PDemoWindow = ^TDemoWindow;    { a pointer to type TDemoWindow }
  42.       TDemoWindow = OBJECT(TWindow)  { a simple user-defined descendant }
  43.       END;                           { object of TWindow }
  44.  
  45.  Hence Window points to an object type TDemoWindow, which inherits the
  46.  fields and methods from the ancestor object type TWindow (see pages
  47.  321-325).  The 'Init' method for TWindow is a static method and is defined
  48.  on page 322.  Three parameters are required, namely the bounds, a title and
  49.  a number.
  50.  The bounds are given by the parameter R, which is of type TRect (see page
  51.  278), whose data fields define the corners at the top left (A) and the
  52.  bottom right (B) of the rectangle.  One of TRect's (static) methods is the
  53.  Assign procedure, which gives the coordinates to the points A and B as XA,
  54.  YA, XB, YB (each of type integer).
  55.  
  56.  The assignment is made using the conventional dot notation, so as on page
  57.  32 the statement is:
  58.  
  59.       R.Assign(0, 0, 26, 7);      { set initial size and position }
  60.  
  61.  The second parameter to a TWindow initialization is a literal title (in
  62.  single quotes) and this will be displayed at the top of the window, along
  63.  with the third parameter, the WinCount number.
  64.  
  65.  The declarations and assignments shown in the above few paragraphs
  66.  illustrate the existence in Turbo Vision of predefined objects, each with
  67.  its appropriate fields and methods, some of which are static and may be
  68.  used directly, whilst others are virtual and may be overridden.
  69.  
  70.  The way in which objects inherit from ancestor types is indicated in the
  71.  Turbo Vision object hierarchy shown on page 66 of the Turbo Vision Guide.
  72.  It is seen that the object type TWindow referred to in the preceding
  73.  paragraphs is part of this hierarchy tree:
  74.  
  75.       TObject --- TView -----
  76.                |          |--
  77.                |          |--
  78.                |          |--
  79.                |          |-- TGroup -----
  80.                                        |--
  81.                                        |-- TWindow -----
  82.                                                      |--
  83.  
  84.  Each of these object types is sufficiently defined for any user application
  85.  in Chapter 13 'Object reference' on pages 205-325.  The actual code is only
  86.  accessible as a compiled unit (.TPU file), the name of which is also shown
  87.  in the reference.  Thus the 'root' ancestor TObject (pages 267-8) is shown
  88.  to be part of OBJECTS.TPU, to have no fields and just three methods, Init,
  89.  Free (both static) and Done (virtual).
  90.  
  91.  Apart from TPoint and TRect (see below) all Turbo Vision's standard objects
  92.  are ultimately derived from TObject.  Any object that uses Turbo Vision's
  93.  streams facilities must trace its ancestry back to TObject.
  94.  
  95.  TView is found in VIEWS.TPU, it has 11 fields, including size and options,
  96.  and 64 methods, including Init, Done, Draw, Show and WriteLine (pages
  97.  306-321).
  98.  
  99.  TGroup is also in VIEWS.TPU, it has 4 fields and 29 methods (pages
  100.  235-244).
  101.  
  102.  Because units are used, it follows that there must be a USES statement at
  103.  the start of any application program to ensure that all the required TPUs
  104.  are called.  Chapter 12 'Unit cross reference' (pages 189-204) gives
  105.  details of all the units used by Turbo Vision.  Types, constants,
  106.  variables, procedures and functions are listed for each unit.
  107.  
  108.  Because of inheritance, it is possible that a method used by any object
  109.  type may not be its own, but that of an ancestor type.  It may therefore be
  110.  necessary to refer back through the hierarchy to find the definition of a
  111.  particular method.
  112.  
  113.  TPoint is a simple object type representing a point on the screen by its
  114.  only two fields X and Y. It has no methods and is thus only a record and is
  115.  the ultimate abstraction as far as screen display is concerned.
  116.  
  117.  TRect has two fields, A and B, both of type TPoint and 9 methods.
  118.  
  119.  Apart from the standard object hierarchy of Turbo Vision, there are a
  120.  number of other elements (types, constants, variables, procedures and
  121.  functions) which are defined in the Turbo Vision units.  These are listed
  122.  in Chapter 14, 'Global reference' on pages 327-384 and include for example:
  123.  
  124.  TYPE      PString       defines a pointer to a string         OBJECTS.TPU
  125.            PtrRec        record of ofs & seg of a pointer      OBJECTS.TPU
  126.  
  127.  CONSTANT  hcXXXX        help context constants                VIEWS.TPU
  128.            ofXXXX        options flags                         VIEWS.TPU
  129.  
  130.  VARIABLE  ScreenHeight  height in lines of current screen     DRIVERS.TPU
  131.            MenuBar       stores a pointer to menu bar          APP.TPU
  132.  
  133.  PROCEDURE DisposeMenu   disposes all elements of the menu     OBJECTS.TPU
  134.            ClearScreen   clears the screen                     DRIVERS.TPU
  135.  
  136.  FUNCTION  NewStatusDef  returns a pointer to a new
  137.                          TStatusDef record                     MENUS.TPU
  138.  
  139.  
  140.  
  141.  Turbo Vision examples.
  142.  ----------------------
  143.  
  144.  Now that the structure of Turbo Vision and the notation has been
  145.  presented, it is possible to proceed with discussion of a specific example,
  146.  which relates to a screen display like that of the Turbo Pascal Integrated
  147.  Development Environment.  The display has a working area called the
  148.  Desktop, which occupies most of the screen, a Menu Bar at the top and a
  149.  Status Line at the bottom, which provides the user with advice on how to
  150.  proceed, even if it is only - Alt-X Exit.
  151.  
  152.  The example is described in detail in Chapter 2 of the Turbo Vision Guide
  153.  (pages 23- 28).  The program is in TVGUID01.PAS which is included with the
  154.  demo programs on the distribution disks and is listed below:
  155.  
  156.  PROGRAM TFirst;
  157.  
  158.  USES App;                            { application objects are in APP.TPU }
  159.  
  160.  TYPE TMyApp = OBJECT(TApplication)   { define new application type leaving }
  161.       END;                            { room for future expansion }
  162.  
  163.  VAR MyApp : TMyApp;                  { create an instance of the new type }
  164.  
  165.  BEGIN
  166.    MyApp.Init                         { set it up }
  167.    MyApp.Run                          { interact with the user }
  168.    MyApp.Done                         { clean up afterwards }
  169.  END.
  170.  
  171.  At present the new object type TMyApp is no different to TApplication, but
  172.  it will be in due course when the methods of TApplication are overridden.
  173.  The default behaviour of a TApplication produces a simple screen with a
  174.  blank menu bar, an empty desktop and a status line with only 'Alt-X Exit'.
  175.  These screen areas have been created by the TApplication (virtual) methods
  176.  InitDeskTop, InitMenuBar and InitStatusLine, which are inherited from
  177.  TProgram, the ancestor type to TApplication (see page 272).  Whereas the
  178.  DeskTop initialization is seldom overridden, the other two are always
  179.  overridden in any meaningful application.
  180.  
  181.  The object type TApplication is a simple 'wrapper' around TProgram and only
  182.  differs from TProgram in its constructor and destructor methods.
  183.  TApplication.Init first initializes all Turbo Visions subsystems (the
  184.  memory, video, event, system error and history list managers) and then
  185.  calls TProgram.Init.  Likewise, TApplication.Done first calls TProgram.Done
  186.  and then shuts down all Turbo Vision subsystems.
  187.  
  188.  Thus the new descendant type TMyApp has an Init method, MyApp.Init, which
  189.  will call the inherited TApplication.Init and then call the initialization
  190.  code specific to the new application, overriding some of the inherited
  191.  virtual methods.  An example of a new virtual method is illustrated in
  192.  TVGUID02.PAS, which has a procedure InitStatusLine; virtual; added to the
  193.  type declaration of TMyApp.
  194.  
  195.  This new initialization procedure (shown on the next page) involves a
  196.  sequence of nested calls to standard Turbo Vision functions NewStatusDef
  197.  and NewStatusKey.  The return values to these functions are PStatusDef,
  198.  which points to a type TStatusDef, and PStatusItem, which points to a type
  199.  TStatusItem, respectively.
  200.  
  201.  The TStatusDef type represents a status line definition and is a record
  202.  type declared in MENUS.TPU as follows:
  203.  
  204.  TStatusDef = RECORD
  205.       Next : PStatusDef;     {points to next TStatusDef in a list or NIL}
  206.       Min, Max : Word;       {define range of help contexts for status line}
  207.       Items : PStatusItem;   {points to a list of status line items or NIL}
  208.    END;
  209.  
  210.  The TStatusItem type represents a status line item that can be visible or
  211.  invisible and is a record type declared in MENUS.TPU as follows:
  212.  
  213.    TStatusItem = RECORD
  214.      Next : PStatusItem;    {points to next TStatusItem in a list or NIL}
  215.      Text : PString;        {points to string containing status item legend}
  216.                             {such as 'Alt-X Exit' or NIL if item invisible }
  217.      KeyCode : Word;        {contains scan code of hot key for item or zero}
  218.      Command : Word;        {contains command event generated when selected}
  219.    END;
  220.  
  221.  The linked lists involved in the above two record types were described in
  222.  the notes on Advanced Data Structures, to which reference should be made
  223.  if necessary (HEAP&PTR.TXT on the Turbo Pascal Tutor diskette).
  224.  
  225.  The NewStatusDef function (page 360) allocates and returns a pointer to a
  226.  new TStatusDef record.  The function declaration is:
  227.  
  228.    FUNCTION NewStatusDef(AMin, AMax: Word; AItems: PStatusItem; ANext:
  229.    PStatusDef): PStatusDef;
  230.  
  231.  The record is initialized with the given parameter values.  Calls to
  232.  NewStatusDef and NewStatusKey can be nested to create entire status line
  233.  definitions in one Pascal statement as shown in the example below.
  234.  
  235.  The NewStatusKey function (page 360) allocates and returns a pointer to a
  236.  new TStatusItem record.  The function declaration is:
  237.  
  238.    FUNCTION NewStatusKey(AText: String; AKeyCode: Word; ACommand: Word;
  239.    ANext: PStatusItem): PStatusItem;
  240.  
  241.  The record is initialized with the given parameter values.  If AText is
  242.  empty, the status item is hidden, but will still provide a mapping from the
  243.  given KeyCode to the given Command.
  244.  
  245.  The important statement in the InitStatusLine procedure involves the
  246.  StatusLine variable of type PStatusLine (page 372) which stores a pointer
  247.  to the application's status line.  TStatusLine is an object type defined in
  248.  MENUS.TPU and described on pages 292-294.  It is initialized as follows:
  249.  
  250.   CONSTRUCTOR Init(var Bounds: TRect; ADefs: PStatusDef);
  251.  
  252.  Thus using the extended form of the New function with the constructor
  253.  invocation as the second parameter, the form of the statement is
  254.  
  255.  StatusLine := New(PStatusLine, Init(VAR Bounds: TRect; ADefs: PStatusDef);
  256.  
  257.  where the Bounds of type TRect is simply R and ADefs of type PStatusDef is
  258.  obtained as the value returned by the function NewStatusDef (see above),
  259.  whilst the third parameter of this function AItems of type PStatusItem is
  260.  obtained as the value returned by the function NewStatusKey (see above).
  261.  In this case the fourth parameter of the function NewStatusKey which is
  262.  ANext of type PStatusItem can itself be obtained as the value returned by
  263.  another call to the function NewStatusKey and so on if necessary until a
  264.  pointer value of NIL is encountered.
  265.  
  266.  Thus the complete InitStatusLine procedure becomes:
  267.  
  268.    PROCEDURE TMyApp.InitStatusLine;
  269.    VAR R: TRect;                      {holds the boundaries of status line}
  270.    BEGIN
  271.      GetExtent(R);                    {set R to coordinates of full screen}
  272.      R.A.Y := R.B.Y - 1;              {move top to one line above bottom}
  273.  
  274.      StatusLine := New(PStatusLine, Init(R,            {create status line}
  275.        NewStatusDef(0, $FFFF,                  {set range of help contexts}
  276.          NewStatusKey('~Alt-X~ Exit', kbAltX, cmQuit,         {define item}
  277.          NewStatusKey('~Alt-F3~ Close', kbAltF3, cmClose,         {another}
  278.          nil)),                                              {no more keys}
  279.        nil)                                                  {no more defs}
  280.      ));                                {bracket closures for Init and New}
  281.    END;
  282.  
  283.  It is also necessary to add PROCEDURE InitStatusLine; VIRTUAL; to the
  284.  declaration of TMyApp.
  285.  
  286.  
  287.  The initialization is a sequence of nested calls to standard Turbo Vision
  288.  functions NewStatusDef and NewStatusKey.  The program defines a status line
  289.  for a range of help contexts from 0 to $FFFF and binds the standard Turbo
  290.  Vision command cmQuit to the Alt-X keystroke and the standard command
  291.  cmClose to the Alt-F3 key.
  292.  
  293.  There is no need for the InitStatusLine method to call the method it
  294.  overrides, TApplication.InitStatusLine, as there is nothing in the
  295.  overridden method that would help.
  296.  
  297.  The part of the string 'Alt-F3 Close' enclosed within the tildes(~) will be
  298.  highlighted on the screen, once a window has been opened and will allow a
  299.  mouse-click activation.  If no window is open then it is not highlighted
  300.  and the cmClose command is disabled by default.
  301.  
  302.  The commands cmQuit and cmClose are standard Turbo Vision commands and do
  303.  not require the user to define them.  Customized commands are declared as
  304.  constant values, in the ranges 100-255 (disable) and 1000-65535
  305.  (non-disable).  Thus the following lines can be added to the above program:
  306.  
  307.       CONST
  308.         cmNewWin = 199;
  309.       .....
  310.       .....
  311.  
  312.          NewStatusKey('~F4~ New', kbF4, cmNewWin,      { bind new command}
  313.       .....
  314.              )                             { close bracket after first NIL}
  315.  
  316.  The Menu Bar is slightly more complicated than the Status Line, because of
  317.  the pull-down sub-menus, but is similarly initialized with nested calls to
  318.  the standard Turbo Vision functions NewMenu, NewSubMenu, NewItem and
  319.  NewLine.
  320.  
  321.  The MenuBar variable (page 355) stores a pointer (of type PMenuView) to the
  322.  application's menu bar (of type TMenuBar), a descendant of TMenuView.  It
  323.  is defined in MENUS.TPU and is initialized as follows:
  324.  
  325.    CONSTRUCTOR Init(VAR Bounds: TRect; AMenu: PMenu);
  326.  
  327.  Thus using the extended form of the New function with the constructor
  328.  invocation as the second parameter, the form of the statement is:
  329.  
  330.    MenuBar := New(PMenuBar, Init(VAR Bounds: TRect; AMenu: PMenu);
  331.  
  332.  where the bounds of type TRect is again R and in this case AMenu, of type
  333.  PMenu, is obtained as the value returned by the function NewMenu (page
  334.  359), which requires a parameter 'Items' of type PMenuItem.  This parameter
  335.  of type PMenuItem is itself returned by the function 'NewSubMenu' (page
  336.  361), which requires as its parameters:
  337.  
  338.       Name: TMenuStr;     { a defined string type of 31 characters - p 378}
  339.       AHelpCtx: Word;     { a help context constant }
  340.       SubMenu: PMenu;     { value returned by function NewMenu - p 359 }
  341.       Next; PMenuItem;    { value returned by function NewItem - p 359 }
  342.  
  343.  The function NewItem is declared as follows:
  344.  
  345.    FUNCTION NewItem(Name, Param: TMenuStr; KeyCode: Word: Command: Word;
  346.    AHelpCtx: Word; Next: PMenuItem): PMenuItem;
  347.  
  348.  from which it is clear that NewItem may call itself for the last parameter.
  349.  
  350.  The nested structure of these various function calls can be illustrated as
  351.  follows:
  352.  
  353.  MenuBar allocation and initialization requires the parameter PMenu from the
  354.  function NewMenu and
  355.  
  356.    Function       Parameter       obtained from         Return value
  357.    ------------------------------------------------------------------
  358.  
  359.    NewMenu        1 - PMenuItem   NewSubMenu            PMenu
  360.  
  361.    NewSubMenu     3 - PMenu       NewMenu               PMenuItem
  362.                   4 - PMenuItem   NewSubMenu (another)
  363.  
  364.    NewItem        5 - PMenuItem   NewItem (another)     PMenuItem
  365.  
  366.  
  367.  Applications to show windows in the desktop area of the screen are
  368.  illustrated in the demonstration programs TVGUID04.PAS to TVGUID10.PAS, but
  369.  contain only a few new concepts.  Apart from Event Handling to be discussed
  370.  later, the following new objects and statements are introduced:
  371.  
  372.  A TWindow object which is a specialized group that typically owns a TFrame
  373.  object, an interior TScroller object and one or two TScrollBar objects.
  374.  These attached subviews provide the "visibility" to the TWindow object.
  375.  The TFrame object provides the border, a place for an optional title and
  376.  number and functional icons (close, zoom, drag).  TWindow objects have the
  377.  "built-in" capability of moving and growing via mouse drag or cursor
  378.  keystrokes.  They can be zoomed and closed via mouse clicks in the
  379.  appropriate icon regions.  They work with scroll bars and scrollers.
  380.  Numbered windows can be selected with ALT-n keys (n = 1 to 9).  TWindow
  381.  initialization requires three parameters: Bounds of type TRect, a title of
  382.  type TTitleStr and a window number.
  383.  
  384.  TDeskTop is a descendant of TGroup and inherits the procedure Insert, which
  385.  requires a parameter of type PView.  Thus the statement to place a window
  386.  in the desktop is: DeskTop^.Insert(Window) where Window is a pointer
  387.  
  388.  Any number of views, called subviews, can be inserted into a group object
  389.  like desktop, which is called the owner view.
  390.  
  391.  To place something into a window, a view called an interior is created and
  392.  then inserted.  An object TInterior, a descendant of TView, with its own
  393.  initialization constructor and own draw procedure is introduced.  The
  394.  WriteStr procedure, inherited from TView, is added to the TInterior.Draw
  395.  procedure.  The TInterior initialization includes the GrowMode variable,
  396.  which ensures that the subview will grow as the owner view is resized.
  397.  TVGUID05.PAS displays a Demo Window, with "Hello World" inserted.
  398.  
  399.  Frequently it will be necessary to create a procedure 'ReadFile' to read a
  400.  file for insertion in a window.  The normal assign and reset statements are
  401.  followed by a while loop to read each line and dynamically allocate heap
  402.  storage, returning a pointer to the first byte of each string, using the
  403.  NewStr function.  This is illustrated in the program TVGUID06.PAS and shown
  404.  on page 39 of the Turbo Vision Guide.
  405.  
  406.  Another procedure 'TInterior.Draw' then uses the standard procedure
  407.  MoveChar (page 358) to move spaces into a buffer, prior to using another
  408.  standard procedure MoveStr (page 358) to move a string into the buffer and
  409.  then uses the WriteLine procedure, inherited from TView, to write the line
  410.  contained in the buffer to the screen (page 321).  This procedure is used
  411.  in TVGUID07.PAS and is shown on page 40 of the Guide.
  412.  
  413.  In order to see the whole file on the screen is necessary to be able to
  414.  scroll up and down, with the mouse reacting with scroll bars at the right
  415.  and bottom of the view.  In this case the object type TInterior is a
  416.  descendant of TScroller, instead of TView, as above.  The new constructor
  417.  invocation has a bounds parameter and also two pointers of type PScrollBar,
  418.  namely HScrollBar and VScrollBar, which point to the horizontal and
  419.  vertical scroll bars respectively.
  420.  
  421.  A procedure TDemoWindow.MakeInterior uses the function 'StandardScrollBar',
  422.  inherited from type TWindow, to create, inset and return a pointer to a
  423.  standard scroll bar for the window.  The constructor invocation for
  424.  TScroller uses the parameters Bounds, HScrollBar and VScrollBar and is
  425.  called within the New function in order to obtain the pointer to the
  426.  'Interior'.  Finally the insert procedure, inherited from TGroup, is called
  427.  to insert the interior.
  428.  
  429.  TVGUID08.PAS provides an illustration of a scrolling interior and is shown
  430.  in part on pages 42-44 of the Guide.
  431.  
  432.  Finally, a program provided on Borland's Install diskette, TVDEMO.PAS, can
  433.  be modified by the user for his own purposes, using the above information
  434.  to create a menu-bar, desk-top and status-line display for any number of
  435.  text files, including source code, etc. The writer of these notes has made
  436.  use of this program for this tutorial display.
  437.  
  438.  
  439.  TVISION.TXT
  440.  20.3.91
  441.  Revised 12.6.93
  442.  
  443.  
  444.